package base.core.security;

import org.springframework.security.core.context.SecurityContextHolder;

import lombok.extern.slf4j.Slf4j;

/**
 * @author Pengwei
 * @description
 * @creatDate 2022/1/14 15:14
 */
@Slf4j
public class TokenUtils {

    /**
     * 获取Token信息
     */
    public static TokenInfo getTokenInfo() {
        try {
            TokenInfo tokenInfo = (TokenInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return tokenInfo;
        } catch (Exception e) {
            log.error("获取token信息错误", e);
        }
        return null;
    }

    /**
     * 获取用户ID
     */
    public static String getUserId() {
        TokenInfo tokenInfo = getTokenInfo();
        if (tokenInfo != null) {
            return tokenInfo.getUserId();
        }
        return null;
    }
}
